package com.nanyang.tms.manager.bill.handler;

import cn.hutool.core.util.ObjectUtil;
import com.nanyang.common.core.flow.context.TransHandlerContext;
import com.nanyang.common.core.flow.handler.AbstractTransHandler;
import com.nanyang.common.tool.utils.convert.BeanUtils;
import com.nanyang.common.tool.utils.uuid.GlobalIdGeneratorUtil;
import com.nanyang.tms.common.contstant.TmsSerializeConstant;
import com.nanyang.tms.common.dict.TmsExceptionEnum;
import com.nanyang.tms.common.exception.TmsManagerException;
import com.nanyang.tms.common.util.TmsExceptionUtil;
import com.nanyang.tms.dao.company.CompanyDao;
import com.nanyang.tms.dao.company.CompanyStoredDao;
import com.nanyang.tms.dao.company.CompanyTransactionRecordDao;
import com.nanyang.tms.domain.bill.context.BillUpdateContext;
import com.nanyang.tms.domain.company.CompanyStoredDO;
import com.nanyang.tms.domain.company.CompanyStoredVO;
import com.nanyang.tms.domain.company.CompanyTransactionRecordDO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.Date;
@Slf4j
@Component
public class BillTransactionRecordHandler extends AbstractTransHandler{
	@Autowired
	private CompanyStoredDao companyStoredDao;
	@Autowired
	private CompanyDao companyDao;
	@Autowired
	private CompanyTransactionRecordDao companyTransactionRecordDao;
	@Autowired
	private GlobalIdGeneratorUtil globalIdGeneratorUtil;

	@Override
	public boolean isAsync() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean handle(TransHandlerContext context) {
		BillUpdateContext billUpdateContext = (BillUpdateContext)context.getInnerContext(BillUpdateContext.class);
		try {

			CompanyStoredDO companyStoredDO=new CompanyStoredDO();
			if(ObjectUtil.isNull(billUpdateContext.getAmount())||billUpdateContext.getAmount().doubleValue()==0){
				return true;
			}else if(billUpdateContext.getAmount().doubleValue()>0){//大于0说明客户扣少了；减少
				companyStoredDO.setUseBalance(billUpdateContext.getAmount().negate());
			}else if(billUpdateContext.getAmount().doubleValue()<0){//小于0说明客户扣多了；增加
				companyStoredDO.setUseBalance(billUpdateContext.getAmount().abs());
			}
			companyStoredDO.setCompanyCode(billUpdateContext.getCompanyCode());
			companyStoredDO.setGmtModifiedPin(billUpdateContext.getPin());
			if(companyStoredDao.updateBalanceAmount(companyStoredDO)<=0){//修改客户储值余额
				throw new TmsManagerException(TmsExceptionEnum.BILL_AMOUNT_ERROR.getCode(), TmsExceptionEnum.BILL_AMOUNT_ERROR.getMessage());
			}
			CompanyStoredVO companyStoredVO = companyStoredDao.selectByCode(billUpdateContext.getCompanyCode());
			if(ObjectUtil.isNull(companyStoredVO)){
				log.error(TmsExceptionEnum.COMPANY_STORE_FAIL.getCode() + TmsExceptionEnum.COMPANY_STORE_FAIL.getMessage());
				throw new TmsManagerException(TmsExceptionEnum.COMPANY_STORE_IS_NOT_EXIST.getCode(), TmsExceptionEnum.COMPANY_STORE_IS_NOT_EXIST.getMessage());
			}
			CompanyTransactionRecordDO record=  buildTransactionRecord(billUpdateContext, companyStoredVO);
			companyTransactionRecordDao.insert(record);

			return true;
		} catch (Exception e) {
			throw TmsExceptionUtil.wrapException(e);
		}
		

	}
	
	private CompanyTransactionRecordDO buildTransactionRecord(BillUpdateContext billUpdateContext,CompanyStoredVO companyStored ){
		String recordSeq = TmsSerializeConstant.TRANSACTION_RECORD_KEY+globalIdGeneratorUtil.getNextSeq(TmsSerializeConstant.TRANSACTION_RECORD_SEQ_KEY, TmsSerializeConstant.SEQ_INCRBY);
		CompanyTransactionRecordDO record=	CompanyTransactionRecordDO.builder()
		.actualBalance(companyStored.getActualBalance())
		.amount(billUpdateContext.getAmount().abs())
		.amountType(billUpdateContext.getAmount().doubleValue()>0?1:0)
		.billCode(billUpdateContext.getWaybillCode())
		.billTime(new Date())
		.billTypeCode("PAYMENT")
		.billTypeName("消费")
		.cardName(companyStored.getCardName())
		.cardNo(companyStored.getCardNo())
		.companyCode(billUpdateContext.getCompanyCode())
		.companyName(companyDao.selectByCode(billUpdateContext.getCompanyCode()).getCompanyName())
		.frozenAmount(companyStored.getFrozenAmount())
		.pin(companyStored.getPin())
		.recordCode(recordSeq)
		.useBalance(companyStored.getUseBalance())
		.userName(companyStored.getUserName())
		.remark("对账单")
		.build();
		record.setIp(billUpdateContext.getIp());
		record.setGmtCreatePin(billUpdateContext.getPin());
		record.setGmtModifiedPin(billUpdateContext.getPin());
		return record;
	}

}
